//
//  Team9DesignVC.m
//  Team9Project
//
//  Created by 朱嘉祺 on 15/10/19.
//  Copyright © 2015年 apple. All rights reserved.
//

#import "Team9DesignVC.h"
#import "UINavigationBar+Awesome.h"
#import "Team9DesignHederImageView.h"
#import "Team9ExpertImagViewCell.h"
#import "Team9DesignPhotoModel.h"
#import "JQAFNetworkTool.h"
#import "Team9ExpertFaceCollectionCell.h"
#import "Team9ExpertSectionHeaderView.h"
#import "Team9ExpertFootCommentView.h"

#import "Team9ExpertCommentModel.h"
#import "Team9ExpertLikeUserModel.h"

@interface Team9DesignVC ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, strong)UITableView *tableView;
@property(nonatomic, strong)NSArray *array;
@property(nonatomic, strong)NSArray<Team9ExpertLikeUserModel *> *users;
@property(nonatomic, strong)NSArray<Team9ExpertCommentModel *> *comments;
@property(nonatomic, strong)Team9DesignHederImageView *imageView;
@end

@implementation Team9DesignVC


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -64, WIDTH, HEGHT + 64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    self.tableView.bounces = YES;
//    NSLog(@"%@", _url);
    
    self.imageView = [[Team9DesignHederImageView alloc] init];
    self.imageView.vc = self;
    _imageView.frame = CGRectMake(0, 0, WIDTH, 300 * WIDTH / 375);
//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 300)];
//    self.tableView.tableHeaderView = _imageView;
   
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    
    
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerClass:[Team9ExpertImagViewCell class] forCellReuseIdentifier:@"Cell"];
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor clearColor]];
    
    self.empty = [Team9EmptyTool toolWithScrollView:self.tableView];
    [self createData];
}
#pragma mark 刷新头视图
- (void)reloadHeader{
    if ([self.imageView.model.col_summary isEqualToString:@""]) {
        self.tableView.tableHeaderView = _imageView;
    } else {
        Team9BaseView * view = [[Team9BaseView alloc] init];
        [self.imageView addSubview:view];
        Team9BaseLabel *sumaryLabel = [[Team9BaseLabel alloc] init];
        [view addSubview:sumaryLabel];
        sumaryLabel.font = [UIFont systemFontOfSize:14 * WIDTH / 375];
        CGFloat height = [Team9Tool heightByFloatSize:14 * WIDTH / 375 width:WIDTH - 40 With:self.imageView.model.col_summary];
        sumaryLabel.text = self.imageView.model.col_summary;
        sumaryLabel.numberOfLines = 0;
        view.frame = CGRectMake(10, 300 * WIDTH / 375 + 10, WIDTH - 20, height + 20);
        sumaryLabel.frame = CGRectMake(10, 10, WIDTH - 40, 0);
        [sumaryLabel sizeToFit];
        _imageView.frame = CGRectMake(0, 0, WIDTH, 300 + WIDTH / 375 + 40 + height);
        self.tableView.tableHeaderView = _imageView;
    }
}
#pragma mark 申请数据
- (void)createData{
    [Team9RefreshTool getUrl:_url success:^(id result) {
        self.tableView.tableFooterView = [Team9BaseView new];
        NSArray *array = [Team9BaseModel baseModelByArr:result[@"data"][@"collection_photos2"][@"photos"]];
        if (array.count) {
            self.array = [Team9DesignPhotoModel baseModelByArr:result[@"data"][@"collection_photos2"][@"photos"][0][@"value"]];
        }
        self.comments = [Team9ExpertCommentModel baseModelByArr:result[@"data"][@"collection_comments"][@"comments"]];
        
        self.users = [Team9ExpertLikeUserModel baseModelByArr:result[@"data"][@"collection_liked_users"][@"users"]];
        Team9ExpertCollection_infoModel *model = [Team9ExpertCollection_infoModel new];
        [model setValuesForKeysWithDictionary:result[@"data"][@"collection_info"][@"info"]];
        self.imageView.model = model;
        [self reloadHeader];
        [self.tableView reloadData];

    } scrollView:self.tableView empty:self.empty];
}
#pragma mark 滚动调整 navigationbar 自己看demo
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    UIColor * color;
    if ([Team9EveningType shareEvening].eveningFailling) {
        color = [UIColor colorWithWhite:0.15 alpha:1];
    } else color = [UIColor colorWithWhite:0.95 alpha:1];
    CGFloat offsetY = scrollView.contentOffset.y + 64;
    if (offsetY > 0) {
        CGFloat alpha = MIN(1, offsetY / (300 * WIDTH / 375 - 64));
        [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:alpha]];
    } else {
        [self.navigationController.navigationBar lt_setBackgroundColor:[color colorWithAlphaComponent:0]];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    self.tableView.delegate = self;
    [self scrollViewDidScroll:self.tableView];
    [self.navigationController.navigationBar setShadowImage:[UIImage new]];
    self.navigationController.navigationBar.translucent = YES;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.tableView.delegate = nil;
    [self.navigationController.navigationBar lt_reset];
    self.navigationController.navigationBar.translucent = NO;
}
#pragma mark UITableViewDatasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    Team9ExpertSectionHeaderView *view = [[Team9ExpertSectionHeaderView alloc] init];
    view.frame = CGRectMake(0, 0, WIDTH, 30);
    if (section == 0) {
        view.titleLabel.text = [NSString stringWithFormat:@"完工照(%ld)", self.array.count];
    }
    if (section == 1) {
        view.titleLabel.text = [NSString stringWithFormat:@"赞过的人(%ld)", self.users.count];
    }
    if (section == 2) {
        view.titleLabel.text = [NSString stringWithFormat:@"评论(%ld)", self.comments.count];
    }
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        if (self.array.count) {
            return 44.0f;
        }
    }
    if (section == 1) {
        if (self.users.count) {
            return 44.0f;
        }
    }
    if (section == 2) {
        if (self.comments.count) {
            return 44.0f;
        }
    }
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
         return self.array.count;
    }
    if (section == 1 && self.users.count) {
        return 1;
    }
    if (section == 2) {
        return self.comments.count;
    }
    return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        static NSString *CellIdentifier = @"Cell";
        Team9ExpertImagViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        //    cell.textLabel.text = @"text";
        cell.model = self.array[indexPath.row];
        return cell;
    }
    if (indexPath.section == 1){
        static NSString *reuse = @"faceCellReuse";
        Team9ExpertFaceCollectionCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
        if (!cell) {
            cell = [[Team9ExpertFaceCollectionCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse];
        }
        cell.VC = self;
        cell.array = self.users;
        return cell;
    }
    if (indexPath.section == 2) {
        Team9ExpertFootCommentView *cell = [tableView dequeueReusableCellWithIdentifier:@"reuseCell"];
        if (!cell) {
            cell = [[Team9ExpertFootCommentView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuseCell"];
        }
        cell.vc = self;
        cell.model = self.comments[indexPath.row];
        return cell;
    }
    return nil;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        Team9DesignPhotoModel *photo = self.array[indexPath.row];
        CGFloat height = (WIDTH - 20) / photo.pic_width.floatValue * photo.pic_height.floatValue;
        return height + 20;
    }
    if (indexPath.section == 1) {
        return 40 * WIDTH / 375 + 20;
    }
    if (indexPath.section == 2) {
        CGFloat width = WIDTH - 100 * WIDTH / 375 - 20;
        Team9ExpertCommentModel *model = self.comments[indexPath.row];
        if (model.comment_parent_id.boolValue) {
            width -= 40 * WIDTH/ 375 + 20;
        }
        CGFloat height = [Team9Tool heightByFloatSize:14 * WIDTH / 375 width:width With:model.content] + 25 * WIDTH / 375 + 20;
        CGFloat lessHeight = 40 * WIDTH / 375 + 20;
        return height > lessHeight ? height : lessHeight;
    }
    return 0;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
